﻿/// <reference path="../../typings/toastr/toastr.d.ts" />


module WorkBasket.Repositories{
    export class RepositoryBase<T>{

        private _Controller: string;

      
        constructor(Controller: string) {
            this._Controller = Controller;
        }

 

        PopulateObjectCallBack = function (oa) {
 
            return oa;
        };

        PopulateSingleCallBack = function (o) {
 
            return o;
        };
 
        UpdateDataCallBack = function (returndata) {
            toastr.info(returndata.Message);
 
   
            return returndata.Object;
        }



        GetAll(callback: Function): Array<Object> {
            if (callback == null || callback == undefined) {
                callback = this.PopulateObjectCallBack;
            
            }
            return this.GetData("GetAll", callback);
        }
        GetByParentKey(Index: any, Command: string = 'GetByParentKey', CallBack?: Function) {
            if (CallBack == undefined) {
                CallBack = this.PopulateObjectCallBack;   
            }



            return this.GetData(Command, CallBack, Index);

        }
        GetByKey(Index: any, callback?: Function,Command: string = 'GetByKey') {
            if (callback == undefined) {
                callback = this.PopulateSingleCallBack;
            }
            return this.GetData(Command, callback, Index);
        }

        Delete(data: any, Key: number, callback?: Function) {
            if (callback == undefined) {
                callback = this.UpdateDataCallBack;
            }
            return this.GetData("Delete", callback, undefined, data, "POST");
        }


        SaveData(data: any,Key?: number,callback?: Function){
 
            if (callback == undefined) {
                callback = this.UpdateDataCallBack;
            }
            //var obj = new Object;
       
            //for (var property in data) {
            //    if (data.hasOwnProperty(property)) {
            //        obj[property] = ko.unwrap(data[property]);
            //    }

            //};


            return this.GetData("Save", callback, undefined, data, "POST");
        }


        GetData(Action: string, callback: Function, Index?: any, Data: any = '', HttpType: string = "GET"): Array<Object>
        {
            var uri: string;
            var oCollection: Array<Object> = null;

            uri = "/api/" + this._Controller;
            if (Action === undefined) {
                uri += "/index";
            } else {
                uri += "/" + Action;
            };
            if (Index !== undefined) {
                uri += "/" + Index;
            };
 
            $.ajax({
                type: HttpType,
                url: uri,
                async: false,
                data: Data,
                success: data => {
                    oCollection = callback(data);
                },
                error: () => {
                    throw new Error('There was an transacting with the api at ' + uri);
                }
            });

            return oCollection;

        }

        //kovalue(obj) {
        //    var ret;
        //    if (ko.isObservable(obj)) {
        //        ret = obj();
        //    } else {
        //        ret = obj;
        //    }
        //    return ret;
        //}

    };
} 